package com.sgcc.pda.zoom;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.google.gson.Gson;
//import com.qihoo360.replugin.RePlugin;
import com.sgcc.pda.hzaidl.IHZAidl;
import com.sgcc.pda.hzaidl.IHZAidlCallback;
import com.sgcc.pda.hzaidl.entity.CommonErrorBean;
import com.sgcc.pda.hzaidl.entity.ResponseBean;
import com.sgcc.pda.hzaidl.error.HZErrorCode;

/**
 * Created by cj on 2019/12/10.
 */
public class HardwareBinderUtil {

    private static final String TAG = "cj";


    private static class Instance {
        private static final HardwareBinderUtil mInstance = new HardwareBinderUtil();
    }

    /**
     * 单例
     */
    public static HardwareBinderUtil getInstance() {
        return HardwareBinderUtil.Instance.mInstance;
    }

    private IHZAidl hzAidl;
    private boolean isBind;


    public boolean bindBinder() {
        /*TODO 已处理：改造时无需引入插件
        IBinder iBinder = RePlugin.fetchBinder("hw", "HwBinder");
        if (iBinder != null) {
            hzAidl = IHZAidl.Stub.asInterface(iBinder);
        }
        return isBind = hzAidl != null;*/
        return false;
    }


    public String post(String data, IHZAidlCallback callback) {
        try {
            return hzAidl.post(data, callback);
        } catch (Exception e) {
            e.printStackTrace();
            callbackFail(callback, HZErrorCode.ERROR_1006);
            return fail(HZErrorCode.ERROR_1006);
        }
    }

    public boolean isBind() {
        return isBind;
    }

    public void unbind() {
        isBind = false;
        hzAidl = null;
    }

    /**
     * 原生aidl绑定
     * 测试硬件服务用
     *
     * @param context: application上下文
     */
    public boolean bindDebugService(Context context) {
        Intent intent = new Intent("com.sgcc.pda.hw.hwdebugservice");
        intent.setPackage("com.sgcc.pda.hw");
        return context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }


    /**
     * 绑定回调
     */
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            hzAidl = IHZAidl.Stub.asInterface(iBinder);
            isBind = hzAidl != null;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            hzAidl = null;
            isBind = false;
        }
    };

    /**
     * 生成返回数据ResponseBean
     *
     * @param retCode 返回码
     * @param msgJson 返回数据json格式
     * @param tag     请求标识
     */
    private ResponseBean createResponseBean(int retCode, String msgJson, String tag) {
        ResponseBean responseBean = new ResponseBean();
        responseBean.setRetCode(retCode);
        responseBean.setMsgJson(msgJson);
        responseBean.setTag(tag);
        return responseBean;
    }

    private String fail(HZErrorCode errorCode) {
        CommonErrorBean commonErrorBean = new CommonErrorBean(errorCode);
        ResponseBean responseBean = createResponseBean(0, new Gson().toJson(commonErrorBean), "");
        return new Gson().toJson(responseBean);
    }

    private void callbackFail(IHZAidlCallback callback, HZErrorCode errorCode) {
        try {
            callback.callback(fail(errorCode));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
